﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
     <!-- 增加一个配置  -->
     <script src="../../../Scripts/config.js" type="text/javascript" > </script>
     <script src="../../../Scripts/QueryString.js" type="text/javascript" > </script>
     <script type="text/javascript" src="../../../Scripts/bootstrap/js/jquery.min.js"></script>
    <link href="../../../Scripts/easyUI/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../../../Scripts/easyUI/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="../../../Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="../../../Scripts/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../../Scripts/easyUI/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="../js/FormDesignerService.js" type="text/javascript"></script>
    <link href="../../../../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .trSP{background-color:#B9D3EE}
    </style>
    <script language="javascript" type="text/javascript">

        //公共方法
        function AjaxServiceGener(param, callback, scope) {
            $.ajax({
                type: "GET", //使用GET或POST方法访问后台
                dataType: "text", //返回json格式的数据
                contentType: "application/json; charset=utf-8",
                url: Handler, //要访问的后台地址
                data: param, //要发送的数据
                async: true,
                cache: false,
                complete: function () { }, //AJAX请求完成时隐藏loading提示
                error: function (XMLHttpRequest, errorThrown) {
                    callback(XMLHttpRequest);
                },
                success: function (msg) {//msg为返回的数据，在这里做数据绑定
                    var data = msg;
                    callback(data, scope);
                }
            });
        }

        $(function () {

            var Ele_TBName = document.getElementById("TB_EnumName");
            Ele_TBName.focus();

            $('#TB_EnumName').blur(function () //文本失去焦点时触发的事件
            {
                var tbName = document.getElementById("TB_EnumName").value;
                var val = $('input:radio[name="RB_QPJP"]:checked').val();
                if (TB_Name == "") {
                    document.getElementById("TB_EnumKey").value = "";
                    return;
                }

                if (val == "ziduanquanpin") {
                    ParseStringToPinYin(tbName, true);                 
                }
                else {
                    ParseStringToPinYin(tbName, false);
                }

            });
            //点击全拼
            $('input[id=RB_QP]').change(function () {

                var tbName = document.getElementById("TB_EnumName").value; 
                ParseStringToPinYin(tbName, true);
            });
            //点击简拼
            $('input[id=RB_JP]').change(function () {
                var tbName = document.getElementById("TB_EnumName").value;
                ParseStringToPinYin(tbName, false);
            });
        });

        var pageData = {};

        //转拼音，并去除开头为数字的字符
        function ParseStringToPinYin(parseString, parseModel) {
            parseString = $.trim(parseString);
            if (parseString != "") {
                $.post(Handler, { action: 'FrmTextBox_ParseStringToPinyin', name: parseString, flag: parseModel }, function (data) {

                    if (data.indexOf('err@') != -1) {
                        alert(data);
                        return;
                    }

                    document.getElementById("TB_EnumKey").value = data;

                });
            }
        }

        //加载 grid 第几页，每页大小。
        function LoadGridData(pageNumber, pageSize) {

            this.pageNumber = pageNumber;
            this.pageSize = pageSize;

            var params = {
                action: "FrmEnumeration_Init",
                FK_MapData: GetQueryString("FK_MapData"),
                DataType: GetQueryString("DataType"),
                PTableModel: GetQueryString("PTableModel"),
                pageNumber: pageNumber,
                pageSize: pageSize
            };

            AjaxServiceGener(params, LoadDataGridCallBack, this);
        }

        //初始化
        $(function () {

            LoadGridData(1, 6);
        });

        //加载grid后回调函数
        function LoadDataGridCallBack(js, scorp) {

            if (js.indexOf('err@') == 0) {
                alert(js);
                return;
            }

            $("#pageloading").hide();
            if (js == "")
             js = "[]";

            //系统错误
            if (js.status && js.status == 500) {
                $("body").html("<b>访问页面出错，请联系管理员。" + JSON.stringify(js) + "<b>");
                return;
            }

            var pushData = eval('(' + js + ')');

            //获取两个实体.
            var sysEnumMains = pushData["SysEnumMains"];
            var fields = pushData["Fields"];

            var numEnum = 0;


            for (var i = 0; i < sysEnumMains.length; i++) {

                var sysEnumMain = sysEnumMains[i];

                //如果需要字段从集合里选择，排除没有的字段.
                if (fields != null) {

                    var isHave = false; //是否存在.
                    for (var idx = 0; idx < fields.length; idx++) {

                        var field = fields[idx];

                        if (field.FType.indexOf('int') == -1) {
                            continue;
                        }

                        if (field.FName != sysEnumMain.No) {
                            continue;
                        }

                        isHave = true;
                        break;
                    }
                    if (isHave == false)
                        continue;
                }

                numEnum++;

                var trStr = "<tr onclick=getTr('" + sysEnumMain.No + "','" + sysEnumMain.Name + "','" + sysEnumMain.CfgVal + "'," + i + ") >";
                trStr += "<td class=Idx>" + i + "</td>";
                trStr += "<td>" + sysEnumMain.No + "</td>";
                trStr += "<td>" + sysEnumMain.Name + "</td>";
                trStr += "<td>" + sysEnumMain.CfgVal + "</td></tr>";
                trStr += "</tr>";

                $("#newsGrid tr:last").after(trStr);
            }

            if (numEnum == 0) {
                //   alert('错误:已经没有可以用的字段.');
            }

            /*$('#newsGrid').datagrid({
                columns: [[
                    { field: 'No', title: '编号', width: 80 },
                    { field: 'Name', title: '名称', width: 90 },
                    { field: 'CfgVal', title: '枚举值', width: 190 }
                ]],
                idField: 'No',
                singleSelect: true,
                data: pushData,
                width: 'auto',
                height: 'auto',
                striped: true,
                rownumbers: true,
                pagination: true,
                fitColumns: true,
                pageNumber: scorp.pageNumber,
                pageSize: scorp.pageSize,
                pageList: [6, 12, 18, 100],
                loadMsg: '数据加载中......',
                onClickRow: function (rowIndex, rowData) {
                    $('#TB_Name').val(rowData["Name"]);
                    $('#TB_KeyOfEn').val(rowData["No"]);
                    $('#TB_UIBindKey').val(rowData["No"]);
                    $('#TB_Vals').val(rowData["CfgVal"]);

                    //执行编辑函数.
                    Edit(false);
                }
            });
            //分页
            var pg = $("#newsGrid").datagrid("getPager");
            if (pg) {
                $(pg).pagination({
                    onRefresh: function (pageNumber, pageSize) {
                        LoadGridData(pageNumber, pageSize);
                    },
                    onSelectPage: function (pageNumber, pageSize) {
                        LoadGridData(pageNumber, pageSize);
                    }
                });
            }*/
        }
        var rownum = -1;
        function getTr(No, Name, CfgVal, i) {
            $('#TB_Name').val(Name);
            $('#TB_KeyOfEn').val(No);
            $('#TB_UIBindKey').val(No);
            $('#TB_Vals').val(CfgVal);
            if(rownum!=-1)
                $("#newsGrid tr:eq(" + rownum + ")").removeClass("trSP");
            $("#newsGrid tr:eq(" + (parseInt(i) + 1) + ")").addClass("trSP");
            rownum = i + 1;
            Edit(false);
        }

        //传入父窗体
        function GetFrmInfo() {
            return {
                Name: $.trim($("#TB_Name").val()),
                KeyOfEn: $.trim($("#TB_KeyOfEn").val()),
                IsCreateLabel: $("#CB_Generate_Tags").prop("checked"),
                UIBindKey: $.trim($("#TB_UIBindKey").val()),
                Vals: $.trim($("#TB_Vals").val())
            };
        }

        function NewEnum() {

            $("#TB_EnumName").val("");
            $("#TB_EnumKey").val("");
            $("#tabs").tabs('select', '枚举信息');

            var str = '<table>';
            for (var i = 0; i < 21; i++) {

                str += '<tr>';
                str += "<td> " + i + "</td> ";
                str += "<td> <input id='TB" + i + "' class='easyui-textbox' style='width: 200px' /></td> ";
                str += '</tr>';
            }

            var div = this.document.getElementById('DivEnumList');
            div.innerHTML = str;
            pageData.DoType = "FrmEnumeration_NewEnum";
        }

        function Del() {

            var name = $.trim($("#TB_Name").val());
            var key = $.trim($("#TB_KeyOfEn").val());

            if (key == null || key == "") {
                alert('您没有选择行.');
                return;
            }

            var v = window.confirm("您确定要删除枚举值[" + name + " " + key + "]吗？");
            if (v == false)
                return;

            // 定义参数，让其保存到数据库里。
            var param = {
                DoType: "FrmEnumeration_DelEnum",
                EnumKey: key
            };

            $.post(Handler, param, function (data) {

                if (data.indexOf('err@') == 0) {
                    alert(data);
                    return;
                }

                alert(data);
                LoadGridData(1, 6);

            });
        }

        function Edit( isSetTab ) {

            var name =    $.trim($("#TB_Name").val());
            var key =     $.trim($("#TB_KeyOfEn").val());
            var cfgVal =  $.trim($("#TB_Vals").val());
            
            if (key == null || key == "") {
                alert('您没有选择行.');
                return;
            }

            $("#TB_EnumName").val(name);
            $("#TB_EnumKey").val(key);

            if (isSetTab) {
                $("#tabs").tabs('select', '枚举信息');
            }

            var cfgVals = cfgVal.split('@');
            var str = '<table>';
            var long = 0;
            var enumCount = cfgVals.length + 10;
            enumCount = enumCount < 21 ? 21 : enumCount;
            for (var i = 0; i < enumCount; i++) {
                var keyValue = [];
                keyValue= $.grep(cfgVals, function (value) {
                    var keyValue = value.split('=');
                    return keyValue.length >= 2 && keyValue[0] == i;
                });
                if (keyValue.length == 0) {
                    str += '<tr>';
                    str += "<td> " + i + "</td> ";
                    str += "<td> <input id='TB" + i + "'  class='easyui-textbox'  value='' class='easyui-textbox' style='width: 200px' /></td> ";
                    str += '</tr>';
                }
                else {
                    var vls = keyValue[0].split('=');
                    var text = vls.slice(1, vls.length).join('=');
                    if (text == undefined) {
                        continue;
                    }
                    str += '<tr>';
                    str += "<td> " + i + "</td> ";
                    str += "<td> <input id='TB" + i + "'  class='easyui-textbox'  value='" + text + "' class='easyui-textbox' style='width: 200px' /></td> ";
                    str += '</tr>';
                    long = i + 1;
                }
            }

            //for (var i = long; i < 21; i++) {
            //    var text ='';
            //    str += '<tr>';
            //    str += "<td> " + i + "</td> ";
            //    str += "<td> <input id='TB" + i + "'  class='easyui-textbox'  value='' class='easyui-textbox' style='width: 200px' /></td> ";
            //    str += '</tr>';
            //}

            var div = this.document.getElementById('DivEnumList');
            div.innerHTML = str;

            pageData.DoType = "FrmEnumeration_SaveEnum";
        }


        //执行保存.
        function SaveEnum() {

            var name =    $("#TB_EnumName").val();
            var keyofEn = $("#TB_EnumKey").val();

            if (name == '' || name == null) {
                alert('枚举中文名称不能为空.');
                return;
            }

            if (keyofEn == '' || keyofEn == null) {
                alert('枚举英文名称不能为空.');
                return;
            }

            var strs = '';
            for (var i = 0; i < 20; i++) {
                var v = $("#TB" + i).val();
                if (v == '' || v == null)
                    continue;
                strs += '@' + i + '=' + v;
            }

            //alert('将要保存' + strs + "  name=" + name + " keyofEn=" + keyofEn);

            // 定义参数，让其保存到数据库里。

            var param = {
                action: "FrmEnumeration_SaveEnum",
                DoType: pageData.DoType,
                EnumName: name,
                EnumKey: keyofEn,
                Vals: strs
            };

            $.post(Handler, param, function (data) {
                if (data == "保存成功") {
                    alert('保存成功,请在列表里选择该枚举.');
                    LoadGridData(1, 6);
                } else {
                    alert(data);
                }
            });

        }
    </script>
</head>
<body class="easyui-layout">
    <div data-options="region:'center'">
        <div id="tabs" class="easyui-tabs" data-options="fit:true ,border:false">
            <div title="选择已有枚举值" style="width: 600px; height: 600px;">
                <div id="tb" style="padding: 3px;">
                    <input type="button" id="Btn_New" class="easyui-button" onclick="NewEnum()" value="新增" />
                    &nbsp;&nbsp;
                    <input type="button" id="Btn_Edit" onclick="Edit(true)" value="编辑" />
                    &nbsp;&nbsp;
                    <input type="button" id="Btn_DELETE" onclick="Del()" value="删除" />
                </div>
                <table id="newsGrid" border="1" cellspacing="0" cellpadding="0" style="width:95%;">
                    <tr>
                        <th>序号</th><th>编号</th><th>名称</th><th>枚举值</th>
                    </tr>
                </table>

                <table>
                    <tr>
                        <td colspan="2">
                            <br />
                            中文名：
                            <input id="TB_Name" class="easyui-textbox" style="width: 150px" />
                            字段英文名：<input id="TB_KeyOfEn" class="easyui-textbox" style="width: 100px" />
                             <input id="CB_Generate_Tags" type="checkbox" checked="checked" /><label for="CB_Generate_Tags">生成标签</label>
                           

                          <input id="TB_UIBindKey" class="easyui-textbox" style="width: 200px; visibility:hidden" />
                          <input id="TB_Vals" class="easyui-textbox" style="width: 200px; visibility:hidden" />

                        </td>
                    </tr>
                </table>
            </div>

            <div title="枚举信息" id="tab_Edit" style="padding: 10px" >

           <table> 
           <tr>
           <td valign="top" >

                 <table>
                    <tr>
                      <td>  标签 </td>
                      <td> <input id="TB_EnumName" class="easyui-textbox" style="width: 200px" />  </td>
                    </tr>
                    <tr>
                      <td>  枚举键值 </td>
                      <td> <input id="TB_EnumKey" class="easyui-textbox" style="width: 200px" />  </td>
                    </tr>
                     
                     <tr>
                     <td colspan="2" >

                     <input id="RB_QP" type="radio" name="RB_QPJP"  value="ziduanquanpin" /><label  for="RB_QP">字段全拼</label>
                     <input id="RB_JP" type="radio" name="RB_QPJP" checked="checked" value="ziduanjianpin" /><label for="RB_JP">字段简拼</label>

                     <input type="button"  value="重置"  onclick="NewEnum()" />
                     <input type="button"  value="保存"  onclick="SaveEnum()" />
                   <!--  <input type="button"  value="删除"  onclick="DelEnum()" />-->
                     
                     </td>
                     </tr>
                 </table>
                 </td>

                  <td valign="top">
                  <div  id='DivEnumList' ></div>
                  </td>
                 </tr>

                 </table>

            </div>
        </div>
    </div>
</body>
</html>
